package com.linedata.acq.metier.norme.fmb;

import java.util.ArrayList;
import java.util.List;

import oracle.forms.jdapi.Block;
import oracle.forms.jdapi.Canvas;
import oracle.forms.jdapi.FormModule;
import oracle.forms.jdapi.Graphics;
import oracle.forms.jdapi.JdapiIterator;

public class GFRM_FMB {

	private FormModule	fmb;

	public GFRM_FMB(FormModule fmb) {
		this.fmb = fmb;
	}

	private String	error	= "";

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error += error;
	}

	/**
	 * EKFR004_1 :: Le nom des frames est GRPH_<<NOM_DU_BLOCK>>
	 * 
	 * @param fmb
	 */
	public void nomFrame() {

		List<String> list1 = new ArrayList<String>();
		List<String> list2 = new ArrayList<String>();

		for (JdapiIterator canvases = fmb.getCanvases(); canvases.hasNext();) {

			Canvas canvas = (Canvas) canvases.next();
			if (!canvas.getName().toUpperCase().equals("TOOLBAR") && !canvas.getName().toUpperCase().equals("FOOTER")) {

				for (JdapiIterator graphicses = canvas.getGraphicses(); graphicses.hasNext();) {

					Graphics graphics = (Graphics) graphicses.next();
					list1.add(graphics.getName().toUpperCase());
					for (JdapiIterator blocks = ((FormModule) fmb).getBlocks(); blocks.hasNext();) {

						Block block = (Block) blocks.next();
						if (graphics.getName().toUpperCase().equals("GRPH_" + block.getName().toUpperCase())) {

							list2.add(graphics.getName().toUpperCase());
						}
					}
				}
			}
		}

		for (int i = 0; i < list2.size(); i++) {
			String str1 = (String) list2.get(i);
			for (int j = 0; j < list1.size(); j++) {
				String str2 = (String) list1.get(j);
				int result = str1.compareTo(str2);
				if (result == 0) {
					list1.remove(j);
				}
			}
		}

		for (String l1 : list1) {
			this.setError("\tEKFR004_1 : " + l1 + "\n");
		}
	}

	/**
	 * EKFR0024_1 :: La propriété Y position est fixée à 12 pour la frame du
	 * premier block
	 * 
	 * @param fmb
	 */
	public void FramePosY() {

		int i = 0;

		for (JdapiIterator canvases = fmb.getCanvases(); canvases.hasNext();) {

			Canvas canvas = (Canvas) canvases.next();
			if (!canvas.getName().toUpperCase().equals("TOOLBAR") && !canvas.getName().toUpperCase().equals("FOOTER")) {

				for (JdapiIterator graphicses = canvas.getGraphicses(); graphicses.hasNext();) {

					Graphics graphics = (Graphics) graphicses.next();
					i++;
					if (i == 1) {

						if (graphics.getYPosition() != 12) {

							this.setError("\tEKFR0024_1 : " + graphics.getName() + "\n");
						}
					}
				}
			}
		}
	}

	/**
	 * EKFR0025 :: La marge de 30 positions est laissée au desssous de la frame
	 * la plus basse
	 * 
	 * @param fmb
	 */
	public void MargeFrameBasse() {

		int i = 0;
		int j = 0;
		int k = 0;

		for (JdapiIterator canvases = fmb.getCanvases(); canvases.hasNext();) {

			Canvas canvas = (Canvas) canvases.next();
			if (!canvas.getName().toUpperCase().equals("TOOLBAR") && !canvas.getName().toUpperCase().equals("FOOTER")) {

				i++;
				if (i == 1) {

					for (JdapiIterator graphicses = canvas.getGraphicses(); graphicses.hasNext();) {

						graphicses.next();
						j++;
					}

					for (JdapiIterator graphicses = canvas.getGraphicses(); graphicses.hasNext();) {

						Graphics graphics = (Graphics) graphicses.next();
						k++;
						if (k == j) {

							if (graphics.getYPosition() + graphics.getHeight() + 25 > 330)
								this.setError("\tEKFR0025_1 : " + graphics.getName() + "\n");
						}
					}
				}
			}
		}
	}

	/**
	 * EKFR028_1 :: Les propriétés X position et largeur de la frame standard
	 * sont fixées respectivement à 10 et 560
	 * 
	 * @param fmb
	 */
	public void XPositionLargeurFrame() {

		for (JdapiIterator canvases = fmb.getCanvases(); canvases.hasNext();) {

			Canvas canvas = (Canvas) canvases.next();
			if (!canvas.getName().toUpperCase().equals("TOOLBAR") && !canvas.getName().toUpperCase().equals("FOOTER")) {

				for (JdapiIterator graphicses = canvas.getGraphicses(); graphicses.hasNext();) {

					Graphics graphics = (Graphics) graphicses.next();
					if (graphics.getXPosition() != 10 && graphics.getWidth() != 560) {

						this.setError("\tEKFR028_1 : " + canvas.getName() + "\t:: " + graphics.getName() + "\n");
					}
				}
			}
		}
	}

	@Override
	public String toString() {
		return this.getError();
	}
}
